package com.achievo.know.web.link;

import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

import com.achievo.know.application.LinkApplication;
import com.achievo.know.domain.FriendLink;
import com.achievo.know.domain.FriendLink.LinkType;
import com.achievo.know.web.myfile.MyfileAction;
import com.achievo.know.web.util.BaseActionSupport;
import com.redhat.redwork.coc.RedworkAction;

public class LinkAction extends BaseActionSupport implements RedworkAction {

	/**
	 * 
	 */
	private static final long serialVersionUID = -1199275793064217787L;
	
	private static final Logger	logger  = LoggerFactory.getLogger(MyfileAction.class);

	public String home(){
		if("IN".equals(curTab))
			links=linkApplication.findLinks(LinkType.IN);
		else
			links=linkApplication.findLinks(LinkType.OUT);
		return "home";
	}
	
	public String showAdd(){
		if(StringUtils.isNotEmpty(id)){
			link=linkApplication.get(Long.parseLong(id));
		}
		return "add";
	}
	
	public String addLink(){
		try{
			if("1".equals(link.getTypeStr()))
				link.setType(LinkType.IN);
			else
				link.setType(LinkType.OUT);
			linkApplication.add(link);
			jsonResult="保存成功!";
		} catch (Exception e) {
			e.printStackTrace();
			logger.error(e.getMessage(),e);
			jsonResult="出错："+e.getMessage();
		}
		return JSON;
	} 
	
	public String editLink(){
		try{
			if("1".equals(link.getTypeStr()))
				link.setType(LinkType.IN);
			else
				link.setType(LinkType.OUT);
			linkApplication.edit(link);
			jsonResult="保存成功!";
		} catch (Exception e) {
			e.printStackTrace();
			logger.error(e.getMessage(),e);
			jsonResult="出错："+e.getMessage();
		}
		return JSON;
	}
	
	public String delBatch(){
		try{
			if(StringUtils.isNotEmpty(ids)){
				String[] idArr=ids.split(",");
				linkApplication.batchDelete(idArr);
				jsonResult="删除成功!";
			}
		} catch (Exception e) {
			e.printStackTrace();
			logger.error(e.getMessage(),e);
			jsonResult="出错："+e.getMessage();
		}
		return JSON;
	}
	
	public String top(){
		try{
			if(StringUtils.isNotEmpty(id)){
				linkApplication.top(Long.parseLong(id));
				jsonResult="删除成功!";
			}
		} catch (Exception e) {
			e.printStackTrace();
			logger.error(e.getMessage(),e);
			jsonResult="出错："+e.getMessage();
		}
		return JSON;
	}
	
	public String up(){
		try{
			if(StringUtils.isNotEmpty(id)){
				linkApplication.up(Long.parseLong(id));
				jsonResult="删除成功!";
			}
		} catch (Exception e) {
			e.printStackTrace();
			logger.error(e.getMessage(),e);
			jsonResult="出错："+e.getMessage();
		}
		return JSON;
	}
	
	public String down(){
		try{
			if(StringUtils.isNotEmpty(id)){
				linkApplication.down(Long.parseLong(id));
				jsonResult="删除成功!";
			}
		} catch (Exception e) {
			e.printStackTrace();
			logger.error(e.getMessage(),e);
			jsonResult="出错："+e.getMessage();
		}
		return JSON;
	}
	
	@Autowired
	private LinkApplication linkApplication;
	private List<FriendLink> links;
	private String curTab="IN";//当前TAB
	private String ids;//选择删除的IDS
	private String jsonResult;//JSON操作结果
	private String code;
	private FriendLink link;
	private String id;
	private String action;
	
	
	
	
	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public String getAction() {
		return action;
	}

	public void setAction(String action) {
		this.action = action;
	}

	public FriendLink getLink() {
		return link;
	}

	public void setLink(FriendLink link) {
		this.link = link;
	}

	public String getParam() {
		return "curTab="+curTab;
	}

	
	public String getCurTab() {
		return curTab;
	}

	public void setCurTab(String curTab) {
		this.curTab = curTab;
	}

	public String getIds() {
		return ids;
	}

	public void setIds(String ids) {
		this.ids = ids;
	}

	public String getJsonResult() {
		return jsonResult;
	}

	public void setJsonResult(String jsonResult) {
		this.jsonResult = jsonResult;
	}

	public String getCode() {
		return code;
	}

	public void setCode(String code) {
		this.code = code;
	}

	public List<FriendLink> getLinks() {
		return links;
	}

	public void setLinks(List<FriendLink> links) {
		this.links = links;
	}

	public void setLinkApplication(LinkApplication linkApplication) {
		this.linkApplication = linkApplication;
	}
	
}
